﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CUREBusinessLogicLayer;
namespace CURE.Handler
{
    /// <summary>
    /// MenuHandler 的摘要说明
    /// </summary>
    public class MenuHandler : IHttpHandler
    {
        MenuBll menuBll = new MenuBll();
        EventLogBll eventLogBll = new EventLogBll();
        public void ProcessRequest(HttpContext context)
        {
            #region 操作参数
            string action=context.Request.Form["action"];
            string delFalg = context.Request.QueryString["action"];
            string menuName = context.Request.Form["txtMenuName"];
            string editMenuId = context.Request.Form["txtMenuId"];
            string delId = context.Request.QueryString["delID"];
            string menuSort = context.Request.Form["txtMenuSort"];
            #endregion
            #region  登录用户id
            string masterID = string.Empty;
            if (HttpContext.Current.Request.Cookies["MWS_User"] != null)
            {
                masterID = HttpContext.Current.Request.Cookies["MWS_User"]["ID"];
            }
            #endregion
            #region 操作
            if (!string.IsNullOrEmpty(action))
            {
                switch (action)
                {
                    case "add":
                        bool isExist = menuBll.SelectMenuByName(menuName);
                        if (isExist)
                        {
                            context.Response.Write(failure("该菜单已存在"));
                        }
                        else
                        {
                            int maxid = menuBll.GetMaxId();
                            int id = maxid + 1;
                            bool addSuccess = menuBll.InsertMenu(id, menuName, menuSort);
                            if (addSuccess)
                            {
                                context.Response.Write(success("SystemManagement/MenuManagement.aspx"));
                                IDictionary<string,object> dic=new Dictionary<string,object>();
                                dic.Add("masterid",masterID);
                                dic.Add("msg", "新增菜单：" + id + "");
                                eventLogBll.InsertEventLog(dic);
                            }
                            else
                            {
                                context.Response.Write(failure("添加失败"));
                            }
                        }
                        break;
                    case "edit":
                        bool exist = menuBll.SelectMenuByIdAndName(editMenuId, menuName);
                        if (exist)
                        {

                        }
                        else
                        {
                            bool upSuccess = menuBll.UpdateMenu(editMenuId, menuName,menuSort);
                            if (upSuccess)
                            {
                                context.Response.Write(success("SystemManagement/MenuManagement.aspx"));
                                IDictionary<string, object> dic = new Dictionary<string, object>();
                                dic.Add("masterid", masterID);
                                dic.Add("msg", "修改菜单：" +editMenuId + "");
                                eventLogBll.InsertEventLog(dic);
                            }
                            else
                            {
                                context.Response.Write(failure("修改失败"));
                            }
                        }
                        break;
                }
            }
            else if (!string.IsNullOrEmpty(delFalg))
            {
                bool delSuccess = menuBll.DelMenuById(delId);
                if (delSuccess)
                {
                    context.Response.Write(success1());//删除成功
                    IDictionary<string, object> dic = new Dictionary<string, object>();
                    dic.Add("masterid", masterID);
                    dic.Add("msg", "删除菜单：" + delId + "");
                    eventLogBll.InsertEventLog(dic);
                }
                else
                {
                    context.Response.Write(failure());
                }
            }
            else
            {
                context.Response.Write(failure("无操作指定"));
            }
            #endregion
        }
        #region 提示信息
        /// <summary>
        /// 操作失败(无参数)
        /// </summary>
        /// <returns></returns>
        public string failure()
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
        }
        /// <summary>
        /// 操作失败(有参数)
        /// </summary>
        /// <param name="str">错误提示</param>
        /// <returns></returns>
        public string failure(string str)
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
        }
        /// <summary>
        /// 操作成功(无参数)
        /// </summary>
        /// <returns></returns>
        public string success()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\"}";
        }

        public string success1()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
        }

        /// <summary>
        /// 操作成功(有参数)
        /// </summary>
        /// <param name="str">信息</param>
        /// <param name="navTabId">页面指向</param>
        /// <returns></returns>
        public string success(string str, string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
        }
        public string success(string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\",\"navTabId\":\"" + navTabId + "\"}";
        }
        #endregion
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}